package com.my.study.atomic;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * @author Carlos
 * @version 1.0
 * @Description 获取Unsafe类
 * @date 2021/6/4 21:47
 **/
public class UnsafeAccessor {

    private static Unsafe unsafe;

    static {
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            unsafe  = (Unsafe) theUnsafe.get(null);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new Error(e);
        }
    }

    public static Unsafe getUnsafe() {
        return unsafe;
    }


}
